/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package visao;

import alice.tuprolog.Prolog;
import alice.tuprolog.SolveInfo;
import alice.tuprolog.Theory;
import java.awt.Color;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author charles.salles
 */
public class Inicio extends javax.swing.JFrame {

    /**
     * Creates new form Inicio
     */
    private ArrayList<JPanel> listaJanela;
    private ArrayList<JLabel> listaLabel;
    private ArrayList<JPanel> listacategoria;
    private JPanel panelFundoAtual;
    private int contador;
    private int contador_categoria;
    private Janela1 janela1;
    private Janela2 janela2;
    private Janela3 janela3;
    private Janela4 janela4;
    private Janela5 janela5;
    private Limpeza_e_Higiene limpeza_e_Higiene;
    private Bebidas bebidas;
    private Padaria padaria;
    private Mercearia mercearia;
    private Carnes carnes;
    private Hortifruti hortifruti;
    private LinkedList<Object> lista;
    private String acao_limpeza;
    private String categoria;
    
    

    public Inicio() {
        initComponents();
        setLocationRelativeTo(null);
        this.lista = new LinkedList<>();
        this.listacategoria = new ArrayList<>();
        this.janela1 = new Janela1();
        this.janela2 = new Janela2();
        this.janela3 = new Janela3();
        this.janela4 = new Janela4();
        this.limpeza_e_Higiene = new Limpeza_e_Higiene(this.acao_limpeza);
        this.bebidas = new Bebidas();
        this.padaria = new Padaria();
        this.mercearia = new Mercearia();
        this.carnes = new Carnes();
        this.hortifruti = new Hortifruti();


        this.listaJanela = new ArrayList<>();
        this.listaJanela.add(janela1);
        this.listaJanela.add(janela2);
        this.listaJanela.add(janela3);
        this.listaJanela.add(janela4);
        this.listaJanela.add(janela5);
        this.listacategoria.add(limpeza_e_Higiene);
        this.listacategoria.add(bebidas);
        this.listacategoria.add(padaria);
        this.listacategoria.add(mercearia);
        this.listacategoria.add(carnes);
        this.listacategoria.add(hortifruti);


        this.listaLabel = new ArrayList<>();
        this.listaLabel.add(this.labelPasso1);
        this.listaLabel.add(this.labelPasso2);
        this.listaLabel.add(this.labelPasso3);
        this.listaLabel.add(this.labelPasso4);
        this.listaLabel.add(this.labelPasso5);

        this.contador = 0;
        this.contador_categoria = 0;
        this.panelFundoAtual = this.listaJanela.get(this.contador);
        this.panelFundoAtual.setSize(1000, 1000);
        this.panelFundo.add(panelFundoAtual);
        atualizaLabel();
//        IniciaProlog();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelPasso1 = new javax.swing.JLabel();
        labelPasso2 = new javax.swing.JLabel();
        labelPasso3 = new javax.swing.JLabel();
        labelPasso4 = new javax.swing.JLabel();
        labelPasso5 = new javax.swing.JLabel();
        panelFundo = new javax.swing.JPanel();
        buttonAnterior = new javax.swing.JButton();
        buttonProximo = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Sistema Especialista");

        labelPasso1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelPasso1.setText("Descrição");
        labelPasso1.setOpaque(true);

        labelPasso2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelPasso2.setText("Vencimento");
        labelPasso2.setOpaque(true);

        labelPasso3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelPasso3.setText("Categoria");
        labelPasso3.setOpaque(true);

        labelPasso4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelPasso4.setText("Subcategoria");
        labelPasso4.setOpaque(true);

        labelPasso5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelPasso5.setText("Resultado");
        labelPasso5.setOpaque(true);

        javax.swing.GroupLayout panelFundoLayout = new javax.swing.GroupLayout(panelFundo);
        panelFundo.setLayout(panelFundoLayout);
        panelFundoLayout.setHorizontalGroup(
            panelFundoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1637, Short.MAX_VALUE)
        );
        panelFundoLayout.setVerticalGroup(
            panelFundoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 750, Short.MAX_VALUE)
        );

        buttonAnterior.setText("Anterior");
        buttonAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAnteriorActionPerformed(evt);
            }
        });

        buttonProximo.setText("Próximo");
        buttonProximo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonProximoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonAnterior, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 1407, Short.MAX_VALUE)
                        .addComponent(buttonProximo, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(panelFundo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelPasso1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(labelPasso2, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(labelPasso3, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(labelPasso4, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(labelPasso5, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPasso1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPasso2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPasso3, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPasso4, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPasso5, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(panelFundo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(buttonAnterior, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
                    .addComponent(buttonProximo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAnteriorActionPerformed
        // TODO add your handling code here:
        this.contador--;
        if (this.contador < 0) {
            this.contador = 0;
        } else {
            atualizaJanela();
        }
//        if (this.contador == 0) {
//            this.buttonAnterior.setVisible(false);
//        }
        //System.out.println(this.contador);
    }//GEN-LAST:event_buttonAnteriorActionPerformed

    private void buttonProximoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonProximoActionPerformed
        // TODO add your handling code here:
        this.contador++;

        if (this.contador > listaJanela.size() - 1) {
            this.contador = listaJanela.size() - 1;
        } else {
            switch (this.contador) {

                case 1:
                    this.lista.add(this.janela1.nome);
                    this.lista.add(this.janela1.preco);
                    System.out.println(this.janela1.nome);
                    atualizaJanela();
                    break;
                case 2:
                    this.lista.add(this.janela2.acao);
                    atualizaJanela();
                    break;
                case 3:
                    this.lista.add(this.janela3.acao);
                    
                    atualizaCategoria();
                    switch(this.janela3.contador){
                        case 1:
                            break;
                    }
                    System.out.println(this.janela3.acao);
                    categoria = janela3.acao.toLowerCase();
                    IniciaProlog();
                    break;
                case 4:
                    
                    results();
                    break;
                case 5:
                    System.out.println("caso 5");
            }
        }




        //System.out.println(this.contador);
    }//GEN-LAST:event_buttonProximoActionPerformed

    private void atualizaJanela() {
        atualizaLabel();
        this.panelFundo.remove(this.panelFundoAtual);
        this.panelFundoAtual = this.listaJanela.get(this.contador);
        this.panelFundoAtual.setSize(1000, 1000);
        //this.panelFundoAtual.setBackground(Color.red);
        this.panelFundo.add(panelFundoAtual);
        this.panelFundo.updateUI();
    }

    private void atualizaCategoria() {
        atualizaLabel();
        this.panelFundo.remove(this.panelFundoAtual);
        this.panelFundoAtual = this.listacategoria.get(this.janela3.contador);
        System.out.println(this.janela3.contador);
        this.panelFundoAtual.setSize(1000, 1000);
        this.panelFundo.add(panelFundoAtual);
        this.panelFundo.updateUI();
    }

    private void atualizaLabel() {
        for (int i = 0; i < this.listaLabel.size(); i++) {
            if (i == this.contador) {
                this.listaLabel.get(i).setBackground(Color.YELLOW);
            } else {
                this.listaLabel.get(i).setBackground(Color.BLACK);
            }
        }
    }

    public void results(){
        atualizaLabel();
        this.panelFundo.remove(this.panelFundoAtual);
        this.panelFundoAtual = this.listaJanela.get(3);
        
        this.panelFundoAtual.setSize(1000, 1000);
        //this.panelFundoAtual.setBackground(Color.red);
        this.panelFundo.add(panelFundoAtual);
        this.panelFundo.updateUI();
    }
    
    
    public void IniciaProlog() {

        Prolog engine = new Prolog();
        Theory conhecimento = null;

        try {
            conhecimento = new Theory(new FileInputStream("Trabalho-de-IA.pl"));


            engine.addTheory(conhecimento);
            String consulta = "progenitor(Z,X),";
            consulta += "progenitor(X,cervejas).";
            String resultado;
            int pc;

//        computadoresEscolhidos.clear();

            SolveInfo info = engine.solve(consulta);
            
            while (info.isSuccess()) {
//                System.out.println(info.toString());
                resultado = info.getVarValue("Z").toString();
                System.out.println(resultado);
                //pc = Integer.parseInt(resultado);
//                computadoresEscolhidos.add(computadores.get(pc - 1));
                //System.out.println(info.getSolution().toString());
                if (engine.hasOpenAlternatives()) {
                    info = engine.solveNext();
                } else {
                    
                    break;
                }
                
            }
        } catch (Exception ex) {
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Inicio().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAnterior;
    private javax.swing.JButton buttonProximo;
    private javax.swing.JLabel labelPasso1;
    private javax.swing.JLabel labelPasso2;
    private javax.swing.JLabel labelPasso3;
    private javax.swing.JLabel labelPasso4;
    private javax.swing.JLabel labelPasso5;
    private javax.swing.JPanel panelFundo;
    // End of variables declaration//GEN-END:variables
}
